﻿using QuanLyKho.DTO;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace QuanLyKho.DAL
{
    class NhaPhanPhoiDAO : DataProvider
    {
        public ArrayList GetListNhaPhanPhoi()
        {
            string sqlString = "Select * from NhaPhanPhoi";
            adapter = new OleDbDataAdapter(sqlString, connection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);

            ArrayList listNhaPhanPhoi = ConvertDataSetToArrayList(dataSet); ;
            return listNhaPhanPhoi;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            NhaPhanPhoiDTO nhaPhanPhoiDTO = new NhaPhanPhoiDTO();
            nhaPhanPhoiDTO.Ma = dt.Rows[i]["MaNhaPhanPhoi"].ToString();
            nhaPhanPhoiDTO.Ten = dt.Rows[i]["TenNhaPhanPhoi"].ToString();
            nhaPhanPhoiDTO.DiaChi = dt.Rows[i]["DiaChi"].ToString();
            return (object)nhaPhanPhoiDTO;
        }

        public void insert(NhaPhanPhoiDTO nhaPhanPhoiDTO)
        {
            string sqlString = "insert into NhaPhanPhoi values('"
                + nhaPhanPhoiDTO.Ma + "','"
                + nhaPhanPhoiDTO.Ten + "','"
                + nhaPhanPhoiDTO.DiaChi + "','"
                + 0 + "')";
            ExecuteNonQuery(sqlString);
        }

        public ArrayList GetListNhaPhanPhoiByMaHangAndMaKho(string maHang, string maKho)
        {
            string sqlString = "Select DISTINCT NPP.MaNhaPhanPhoi, NPP.TenNhaPhanPhoi, NPP.DiaChi "
                + "FROM NhaPhanPhoi NPP, PhieuNhap PN"
                + " WHERE PN.MaHang = '" + maHang + "' AND PN.MaKho = '" + maKho
                + "' AND NPP.MaNhaPhanPhoi = PN.MaNhaPhanPhoi";

            adapter = new OleDbDataAdapter(sqlString, connection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);

            ArrayList listNhaPhanPhoi = ConvertDataSetToArrayList(dataSet); ;
            return listNhaPhanPhoi;
        }
    }
}
